package com.example.pclimb.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

import com.example.pclimb.model.UserLike;
import com.example.pclimb.repository.UserLikeRepository;

/**
 * 用户点赞视图模型，负责处理用户点赞相关的UI数据
 */
public class UserLikeViewModel extends AndroidViewModel {
    private UserLikeRepository repository;

    public UserLikeViewModel(@NonNull Application application) {
        super(application);
        repository = new UserLikeRepository(application);
    }

    /**
     * 添加用户点赞记录
     * @param userId 用户ID
     * @param guideId 攻略ID
     */
    public void like(int userId, int guideId) {
        UserLike userLike = new UserLike(userId, guideId);
        repository.insert(userLike);
    }

    /**
     * 删除用户点赞记录
     * @param userId 用户ID
     * @param guideId 攻略ID
     */
    public void unlike(int userId, int guideId) {
        repository.deleteUserLike(userId, guideId);
    }

    /**
     * 检查用户是否点赞了攻略
     * @param userId 用户ID
     * @param guideId 攻略ID
     * @return 是否点赞的LiveData
     */
    public LiveData<Integer> isLiked(int userId, int guideId) {
        return repository.isLiked(userId, guideId);
    }

    /**
     * 获取攻略的点赞数
     * @param guideId 攻略ID
     * @return 点赞数的LiveData
     */
    public LiveData<Integer> getLikeCount(int guideId) {
        return repository.getLikeCount(guideId);
    }

    /**
     * 获取用户的点赞数
     * @param userId 用户ID
     * @return 点赞数的LiveData
     */
    public LiveData<Integer> getUserLikeCount(int userId) {
        return repository.getUserLikeCount(userId);
    }

    /**
     * 切换点赞状态（点赞/取消点赞）
     * @param userId 用户ID
     * @param guideId 攻略ID
     * @param currentLiked 当前是否已点赞
     */
    public void toggleLike(int userId, int guideId, boolean currentLiked) {
        if (currentLiked) {
            unlike(userId, guideId);
        } else {
            like(userId, guideId);
        }
    }
} 